//
//  XQOrderCell.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/4.
//

import UIKit

class XQOrderCell: UITableViewCell {
    
    
    var protocolHandle: (() -> Void)?
    
    var model : XQOrderReadingsModel? {
        didSet {
            
            if let url = model?.coarseness {
                self.logoImageView.kf.setImage(with: URL(string: url))
            }
            
            self.titleLb.text = model?.flashlight ?? ""
            self.dollorLb.text = model?.bring ?? ""
            
            
            if (model?.unexplained?.count != 0) {
                self.protocolBtn.isHidden = false
                self.protocolLine.isHidden = false
                self.protocolBtn.setTitle(model?.unexplained, for: .normal)
            }else {
                self.protocolBtn.isHidden = true
                self.protocolLine.isHidden = true
            }            
            
            self.applyBtn.setTitle(model?.tested ?? "", for: .normal)
            
            let count = model?.steady?.count ?? 0
            
            self.tableView.snp.updateConstraints { make in
                make.height.equalTo(count * 22)
            }
        }
    }
    
    
    private lazy var bgView : UIView = {
       let v = UIView()
        v.layer.cornerRadius = 18
        v.layer.masksToBounds = true
        v.layer.borderColor = UIColor.mainTextColor.cgColor
        v.layer.borderWidth = 1
        return v
    }()
    
    private lazy var logoImageView : UIImageView = {
        let imageView = UIImageView()
        imageView.layer.cornerRadius = 5.86
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    private lazy var titleLb : UILabel = {
       let lb = UILabel()
        lb.textColor = .xq_black
        lb.font = UIFont.systemFont(ofSize: 15, weight: .medium)
        return lb
    }()
    
    private lazy var dollorLb : UILabel = {
        let lb = UILabel()
         lb.textColor = .xq_black
         lb.font = UIFont.systemFont(ofSize: 22, weight: .bold)
         return lb
    }()
    
    private lazy var tableView : UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.separatorStyle = .none
        tableView.isScrollEnabled = false
        tableView.register(XQOrderTimeCell.self, forCellReuseIdentifier: "XQOrderTimeCell")
        tableView.isUserInteractionEnabled = false
        return tableView
    }()
    
    private lazy var bottomView : UIView = {
       let view = UIView()
        view.backgroundColor = .mainTextColor
        return view
    }()
    
    private lazy var protocolBtn : UIButton = {
       let btn = UIButton()
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        btn.addTarget(self, action: #selector(protocolClick), for: .touchUpInside)
        return btn
    }()
    
    private lazy var protocolLine : UIView = {
        let v = UIView()
        v.backgroundColor = .white
        return v
    }()
    
    private lazy var applyBtn : UIButton = {
       let btn = UIButton()
        btn.setTitleColor(.mainTextColor, for: .normal)
        btn.backgroundColor = .white
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 11, weight: .medium)
        btn.layer.cornerRadius = 13.5
        btn.isEnabled = false
        return btn
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
       super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        makeUI()
        makeConstraints()
    }

    required init?(coder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
    }
    
    private func makeUI() {
        self.contentView.addSubview(self.bgView)
        self.bgView.addSubview(self.logoImageView)
        self.bgView.addSubview(self.titleLb)
        self.bgView.addSubview(self.dollorLb)
        self.bgView.addSubview(self.tableView)
        self.bgView.addSubview(self.bottomView)
        self.bottomView.addSubview(self.protocolBtn)
        self.bottomView.addSubview(self.protocolLine)
        self.bottomView.addSubview(self.applyBtn)
    }
    
    private func makeConstraints() {
        
        self.bgView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.bottom.equalTo(-15)
        }
        
        self.logoImageView.snp.makeConstraints { make in
            make.top.equalTo(15)
            make.left.equalTo(10)
            make.width.height.equalTo(30)
        }

        self.titleLb.snp.makeConstraints { make in
            make.centerY.equalTo(self.logoImageView)
            make.left.equalTo(self.logoImageView.snp.right).offset(5)
        }
        
        self.dollorLb.snp.makeConstraints { make in
            make.centerY.equalTo(self.logoImageView)
            make.left.greaterThanOrEqualTo(self.titleLb.snp.right).offset(12)
            make.right.equalTo(-10)
        }
        
        self.tableView.snp.makeConstraints { make in
            make.top.equalTo(self.logoImageView.snp.bottom).offset(12)
            make.left.right.equalTo(0)
            make.height.equalTo(0)
        }
        self.bottomView.snp.makeConstraints { make in
            make.top.equalTo(self.tableView.snp.bottom)
            make.left.right.bottom.equalTo(0)
            make.height.equalTo(51)
        }
        self.protocolBtn.snp.makeConstraints { make in
            make.top.equalTo(18)
            make.left.equalTo(10)
            make.height.equalTo(12)
        }
        self.protocolLine.snp.makeConstraints { make in
            make.top.equalTo(self.protocolBtn.snp.bottom)
            make.left.equalTo(self.protocolBtn.snp.left)
            make.right.equalTo(self.protocolBtn.snp.right)
            make.height.equalTo(1)
        }
        self.applyBtn.snp.makeConstraints { make in
            make.right.equalTo(-10)
            make.centerY.equalToSuperview()
            make.width.equalTo(87)
            make.height.equalTo(27)
        }
    }
    
    @objc private func protocolClick() {
        protocolHandle?()
    }

}

extension XQOrderCell : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.model?.steady?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "XQOrderTimeCell", for: indexPath) as! XQOrderTimeCell
        
        if let list = model?.steady {
            if (indexPath.row < list.count) {
                cell.model = list[indexPath.row]
            }
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 22
    }
}
